package com.xcity.game.buycount;

import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.common.Cause;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.vip.VipTemplate;
import com.xcity.game.vip.VipUtils;

import naga.x.game.time.TimeUpdater;

public enum BuyCount {

	/** 0体力 */
	STRENGTH(PlayerPool.PROPERTY_LAST_BUY_STRENGTH_DAY, PlayerPool.PROPERTY_BOUGHT_STRENGTH_TIMES, Cause.BUY_STRENGTH) {
		
		@Override
		public int getMaxCount(Player player) {
			VipTemplate vt = VipUtils.getTemplate(player.getVipLvl());
			return vt.getBuyStrNum();
		}
		
		@Override
		public int getBuyValuePerTime() {
			return Player.STRENGTH_RESTORE_VALUE;
		}
		
		@Override
		void addValue0(Player player) {
			player.addStrength2(getBuyValuePerTime(), cause);
		}
		
	},
	
	/** 1精力 */
	VIGOUR(PlayerPool.PROPERTY_BUY_VIGOUR_DAY, PlayerPool.PROPERTY_BUY_VIGOUR_COUNT, Cause.BUY_VIGOUR) {
		@Override
		public int getMaxCount(Player player) {
			VipTemplate vt = VipUtils.getTemplate(player.getVipLvl());
			return vt.getFullVigourCount();
		}
		
		@Override
		public int getBuyValuePerTime() {
			return Player.VIGOUR_RESTORE_VALUE;
		}

		@Override
		void addValue0(Player player) {
			player.addVigour(getBuyValuePerTime(), cause);
		}
	},
	
	/** 2现金 */
	MONEY(PlayerPool.PROPERTY_BUY_MONEY_DAY, PlayerPool.PROPERTY_BUY_MONEY_COUNT, Cause.BUY_MONEY) {
		@Override
		public int getMaxCount(Player player) {
			VipTemplate vt = VipUtils.getTemplate(player.getVipLvl());
			return vt.getBuyMoneyCount();
		}
		
		@Override
		public int getBuyValuePerTime() {
			return Player.MONEY_RESTORE_VALUE;
		}

		@Override
		void addValue0(Player player) {
			player.addGold(getBuyValuePerTime(), cause);
		}
	},
	
	/** 3重置普通关卡 */
	PVE_NORMAL_RESET {
		@Override
		public int getMaxCount(Player player) {
			VipTemplate vt = VipUtils.getTemplate(player.getVipLvl());
			return vt.getFullNormalStage();
		}

		@Override
		void addValue0(Player player) { // 不能在BuyCountService中处理
			throw new UnsupportedOperationException();
		}
	},
	
	/** 4重置精英关卡 */
	PVE_HERO_RESET {
		@Override
		public int getMaxCount(Player player) {
			VipTemplate vt = VipUtils.getTemplate(player.getVipLvl());
			return vt.getFullNormalStage();
		}

		@Override
		void addValue0(Player player) { // 不能在BuyCountService中处理
			throw new UnsupportedOperationException();
		}
	},
	
	/** 5技能点数 */
	SKILL_POINTS(PlayerPool.PROPERTY_LAST_BUY_SKILL_POINTS_DAY, PlayerPool.PROPERTY_SKILL_POINTS_BOUGHT_COUNT, Cause.BUY_SKILL_POINTS) {
		@Override
		public int getMaxCount(Player player) {
			VipTemplate vt = VipUtils.getTemplate(player.getVipLvl());
			return vt.getBuySkillPointsCount();
		}

		@Override
		void addValue0(Player player) {
			player.addSkillPoints(getBuyValuePerTime(), cause);
		}

		@Override
		public int getBuyValuePerTime() {
			return GameStaticConfig.skillPointsPerTimeBuy;
		}
		
	},
	;

	// 上次购买日期pool key
	public final String poolKeyLastBoughtDay;
	// 今日已购次数pool key
	public final String poolKeyBoughtCount;
	// 每次消耗钻石
	private int[] costs;
	// log cause
	public final String cause;

	BuyCount() {
		this(null, null, null);
	}
	
	BuyCount(String poolKeyLastBoughtDay, String poolKeyBoughtCount, String cause) {
		this.poolKeyLastBoughtDay = poolKeyLastBoughtDay;
		this.poolKeyBoughtCount = poolKeyBoughtCount;
		this.cause = cause;
	}

	/**
	 * 最大可购买次数
	 * @param player
	 * @return
	 */
	public abstract int getMaxCount(Player player);
	
	/*package*/ abstract void addValue0(Player player);

	/**
	 * 获取购买消耗钻石
	 * @param player
	 * @return
	 */
	public int getCostStone(Player player) {
		if (costs == null || costs.length == 0) {
			return 9999;
		}
		int c = getBoughtCount(player);
		return getCostStone(player, c);
	}
	
	/**
	 * 根据已购次数获取下一次消耗钻石
	 * @param player
	 * @param boughtCount
	 * @return
	 */
	public int getCostStone(Player player, int boughtCount) {
		return boughtCount >= 0 && boughtCount < costs.length ? costs[boughtCount] : costs[costs.length - 1];
	}

	/**
	 * 每次可购买的值
	 * @return
	 */
	public int getBuyValuePerTime() {
		return 1;
	}
	
	/**
	 * 今日已购次数
	 * @param player
	 * @return
	 */
	public int getBoughtCount(Player player) {
		int day = player.getPool().getIntValue(poolKeyLastBoughtDay);
		if (day != TimeUpdater.getInstance().today()) {
			return 0;
		}
		return player.getPool().getIntValue(poolKeyBoughtCount);
	}
	
	/**
	 * 今日剩余可购买次数
	 * @param player
	 * @return
	 */
	public int getLeftBuyCount(Player player) {
		int max = getMaxCount(player);
		int c = getBoughtCount(player);
		c = max - c;
		return c > 0 ? c : 0;
	}
	
	/**
	 * 购买成功加数值
	 * @param player
	 */
	public void addValue(Player player) {
		addValue0(player);
		int today = TimeUpdater.getInstance().today();
		int c = player.getPool().getIntValue(poolKeyBoughtCount);
		if (today != player.getPool().getIntValue(poolKeyLastBoughtDay)) {
			player.poolSet(poolKeyLastBoughtDay, today);
			c = 0;
		}
		player.poolSet(poolKeyBoughtCount, c + 1);
	}
	
	public static void init(List<BuyCountTemplate> templates) {
		int size = templates.size();
		BuyCount[] vals = BuyCount.vals;
		for (BuyCount bc : vals) {
			bc.costs = new int[size];
		}
		for (int i = 0; i < size; i++) {
			BuyCountTemplate bct = templates.get(i);
			BuyCount.STRENGTH.costs[i] = bct.getBuyStrengthStone();
			BuyCount.VIGOUR.costs[i] = bct.getBuyVigourStone();
			BuyCount.MONEY.costs[i] = bct.getBuyMoneyStone();
			BuyCount.PVE_NORMAL_RESET.costs[i] = bct.getBuyNormalStone();
			BuyCount.PVE_HERO_RESET.costs[i] = bct.getBuyHeroStone();
			BuyCount.SKILL_POINTS.costs[i] = bct.getBuySkillPointsStone();
		}
	}
	
	static BuyCount[] vals = values();
	public static BuyCount forCode(int code) {
		BuyCount bc = code >= 0 && code < vals.length ? vals[code] : null;
		if (bc == BuyCount.PVE_NORMAL_RESET || bc == BuyCount.PVE_HERO_RESET) {
			bc = null;
		}
		return bc;
	}

}
